package com.faithbj.process.impl;

import javax.xml.bind.annotation.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * User: xiongyuanwei
 * Date: 12-7-14
 * Time: 上午9:45
 */
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlRootElement(name = "entity-mappings")
public class NamedQueries {

    private List<NamedQuery> queries = new LinkedList<NamedQuery>();

    @XmlElement(name = "named-query")
    public List<NamedQuery> getQueries() {
        return queries;
    }

    public void setQueries(List<NamedQuery> queries) {
        this.queries = queries;
    }

    @XmlAccessorType(XmlAccessType.PROPERTY)
    public static class NamedQuery {

        private String name;
        private String query;

        public String applyParams(Map<String, String> params) {
            String ret = query.trim();
            if (params != null) {
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    ret = ret.replace(":" + entry.getKey(), entry.getValue());
                }
            }
            return ret;
        }

        @XmlAttribute
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getQuery() {
            return query;
        }

        public void setQuery(String query) {
            this.query = query;
        }
    }
}
